package cj.web.admin.controllers;

import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.TipInfoDAO;
import cj.web.admin.domain.TipInfo;

@Path("/tipInfo")
public class TipInfoController{

	private static Logger logger = Logger.getLogger(TipInfoController.class.getName());
	
	@Autowired
	private TipInfoDAO tipInfoDao;

	@Get("show")
	public String show(Model model,TipInfo tipInfo)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xwbl);
		return "/views/portal/tipInfoList.vm";
	}

	@Post("getList")
	public String getUserList(TipInfo tipInfo) {
		
		JSONObject jo = new JSONObject();
		
		List<TipInfo> objs = tipInfoDao.query(tipInfo.getTitle(), tipInfo.getStart(), tipInfo.getLength());
		
		int size = tipInfoDao.querysize(tipInfo.getTitle());
		jo.put("data", objs);
		jo.put("draw", tipInfo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("queryById")
	public String queryById(Model model,@Param("id") int id)
	{
		TipInfo obj = tipInfoDao.queryById(id);
		model.add("obj", obj);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xwbl);
		return "/views/portal/tipInfo.vm";
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xwbl);
		return "/views/portal/tipInfoDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,TipInfo tipInfo)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tipInfo.setCreateUser("sys");
			tipInfo.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			tipInfo.setUpdateUser("sys");
			tipInfoDao.create(tipInfo);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		TipInfo obj = tipInfoDao.queryById(id);
		model.add("obj", obj);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xwbl);
		return "/views/portal/callCenterInfoEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,TipInfo tipInfo)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tipInfo.setUpdateUser("sys");
			tipInfoDao.update(tipInfo);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		tipInfoDao.deleteById(id);
		return "r:show";
	}
}
